package com.example.day_day_up.servlet;

import com.example.day_day_up.entity.constants.Constants;
import com.example.day_day_up.entity.dto.CreateImageCode;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/code")
public class CodeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        CreateImageCode vCode = new CreateImageCode(120, 30, 5, 10);
        resp.setHeader("Pragma", "no-cache");
        resp.setHeader("Cache-Control", "no-cache");
        resp.setDateHeader("Expires", 0);
        resp.setContentType("image/jpeg");

        String code = vCode.getCode();
        HttpSession session = req.getSession();
        session.setAttribute(Constants.CODE_KEY, code);
        vCode.write(resp.getOutputStream());
    }
}
